package tests.read;

import static org.junit.Assert.fail;

import java.util.ArrayList;

import model.Budget;
import model.BudgetFactory;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import com.mongodb.BasicDBObject;
import com.mongodb.util.JSON;

/*
 * 
 * Attempts to fetch a budget with a description that does not exist
 * [currently the GUI does not support querying by description]
 * [this test should return false positive]
 * 
 */

public class BudgetQueryTest02
{
	Budget budget;
	BudgetFactory factory;

	@Before
	public void setUp() throws Exception
	{
		factory = new BudgetFactory();
		budget = (Budget) factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is a test budget.");
        budget.commit();
	}

	@After
	public void tearDown() throws Exception
	{
		Helper.delete( budget, "Budgets", WorkletContext.getInstance());
	}

	@Test
	public void test() throws Exception
	{
		BasicDBObject pred = new BasicDBObject("budget.description", "This is not a test budget");
		String criteria = JSON.serialize(pred);
		
		@SuppressWarnings("unchecked")
		ArrayList<Budget> budgets = Helper.query("Budgets", criteria, WorkletContext.getInstance());
		
		try
		{
			if(budgets.size() != 0)
				fail("Querying by false description passed unexpectedly");
		}
		catch (Exception e)
		{
			fail("Fetching budget by false description failed with exception " + e);
		}
	}
}